package logic

import (
	"context"
	"renting-demo/model/mysql"
	"strconv"

	"renting-demo/cart_srv/cart"
	"renting-demo/cart_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// todo: 添加购物车
func (l *AddLogic) Add(in *cart.AddCartRequest) (*cart.Response, error) {
	re := mysql.Search(strconv.Itoa(int(in.GoodsId)), int(in.GoodsNum))
	if re == "0" {
		return &cart.Response{
			Status: "0",
		}, nil
	}
	uid := strconv.Itoa(int(in.UserId))
	gid := strconv.Itoa(int(in.GoodsId))
	mysql.AddCart(uid, gid, int(in.GoodsNum))
	return &cart.Response{
		Status: "1",
	}, nil
}
